package fuzzylizard.teamdocs.web.forms;

import java.io.File;
import java.io.IOException;

import wicket.markup.html.form.Form;
import wicket.markup.html.form.TextField;
import wicket.markup.html.form.upload.FileUpload;
import wicket.markup.html.form.upload.FileUploadField;
import wicket.markup.html.panel.FeedbackPanel;
import wicket.model.PropertyModel;
import wicket.util.lang.Bytes;
import fuzzylizard.teamdocs.Constants;
import fuzzylizard.teamdocs.services.DocumentService;
import fuzzylizard.teamdocs.services.DocumentServiceImpl;
import fuzzylizard.teamdocs.web.wicket.TeamdocsSession;

/**
 * Understands the web form that allows a user to upload a file
 * 
 */
class FileUploadForm extends Form {

    private TeamdocsSession teamdocsSession;
    private FeedbackPanel feedback;
    
    private FileUploadField fileUploadField;
    private String fileName;
    private String summary;
    
    public FileUploadForm(String name, TeamdocsSession teamdocsSession, FeedbackPanel feedPanel) {
        super(name);
        this.teamdocsSession = teamdocsSession;
        this.feedback = feedPanel;
        configureUploadForm();
        addFormFields();
    }

    private void configureUploadForm() {
        setMultiPart(true);
        setMaxSize(Bytes.megabytes(Constants.MAX_UPLOAD_SIZE));
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    public String getFileName() {
        return fileName;
    }
    
    public void setSummary(String summary) {
        this.summary = summary;
    }
    
    public String getSummary() {
        return summary;
    }

    @Override
    protected void onSubmit() {
        final FileUpload upload = fileUploadField.getFileUpload();
        
        if (upload != null) {
            File file = createFile(upload);
            saveFileToTempDirectory(upload, file);
            addFileToRepository(file);
        }
    }
    
    private void addFormFields() {
        add(fileUploadField = new FileUploadField("fileInput"));
        add(new TextField("filename", new PropertyModel(this, "filename")));
        add(new TextField("summary", new PropertyModel(this, "summary")));
    }

    /**
     * Save the file into the repository
     * 
     * @param newFile To be added to repository
     */
    private void addFileToRepository(final File newFile) {
        DocumentService docService = new DocumentServiceImpl();
        docService.saveDocument(teamdocsSession.getDirectory(), teamdocsSession.getUser(), newFile, getFileName(), getSummary());
        feedback.info("Document uploaded successfully");
    }
    
    /**
     * create a new File using the filename plus the temp directory
     * 
     * @param upload Wicket FileUpload object
     * @return new file to be added to repository
     */
    private File createFile(final FileUpload upload) {
        return new File(Constants.TEMP_DIRECTORY, upload.getClientFileName());
    }
    
    /**
     * Save the new file to the temp directory
     * 
     * @param upload Wicket FileUpload object
     * @param newFile The newly added file to be saved
     */
    private void saveFileToTempDirectory(final FileUpload upload, File newFile) {
        try {
            newFile.createNewFile();
            upload.writeTo(newFile);
            feedback.info("saved file: " + upload.getClientFileName());
        } catch (IOException e) {
            throw new IllegalStateException("Unable to write file");
        }
    }
}
